import 'package:flutter/material.dart';
import 'package:flutter_base/bean/mine/collect_list_entity.dart';
import 'package:flutter_base/viewmodel/mine/collect_vm.dart';
import 'package:oktoast/oktoast.dart';
import 'package:provider/provider.dart';

import '../../utils/navigator_utils.dart';
import '../common/webview_page.dart';
import '../common/webview_widget.dart';

///
/// @DIR_PATH：lib/page/mine
/// @TIME：2024/6/23 11:03
/// @AUTHOR：starr
/// 我的收藏界面
class CollectPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => CollectPageState();
}

class CollectPageState extends State<CollectPage> {
  CollectViewModel viewModel = CollectViewModel();

  @override
  void initState() {
    super.initState();
    viewModel.getCollectList();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("我的收藏"),
        ),
        body: ChangeNotifierProvider(create: (context) {
          return viewModel;
        }, child: Consumer<CollectViewModel>(
          builder: (context, vm, child) {
            return SafeArea(
                child: ListView.builder(
              itemBuilder: (BuildContext context, int index) {
                return _listItemView(vm.collectList?[index], index);
              },
              itemCount: vm.collectList?.length ?? 0,
            ));
          },
        )));
  }

  Widget _listItemView(CollectListDatas? itemData, int index) {
    var name = "无名";
    if (itemData?.author.isNotEmpty == true) {
      name = itemData?.author ?? "";
    }
    return GestureDetector(
      onTap: () {
        NavigatorUtils.pushPage(
            context,
            WebViewPage(
              webViewType: WebViewType.URL,
              loadResource: itemData?.link ?? "",
              title: itemData?.title,
              showTitle: true,
            ));
      },
      child: Container(
          margin: const EdgeInsets.all(10),
          padding: const EdgeInsets.all(10),
          decoration: BoxDecoration(
            borderRadius: const BorderRadius.all(Radius.circular(10)),
            border: Border.all(
              color: Colors.grey,
              width: 0.5,
            ),
          ),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Row(
                children: [
                  ClipRRect(
                    borderRadius: BorderRadius.circular(20),
                    child: Image.asset(
                      "assets/images/2.0x/app_icon.png",
                      width: 40,
                      height: 40,
                      fit: BoxFit.fill,
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.only(left: 10),
                    child: Text(name,
                        style: const TextStyle(fontSize: 15, color: Colors.black)),
                  ),
                  const Expanded(child: SizedBox()),
                  Padding(
                    padding: const EdgeInsets.only(right: 10),
                    child: Text(itemData?.niceDate ?? "",
                        style:
                            const TextStyle(fontSize: 14, color: Colors.black)),
                  ),
                ],
              ),
              Padding(
                padding: const EdgeInsets.only(top: 10),
                child: Text(itemData?.title ?? "",
                    style: const TextStyle(fontSize: 18, color: Colors.black)),
              ),
              Row(
                children: [
                  Text(itemData?.chapterName ?? "",
                      style: const TextStyle(
                          fontSize: 12,
                          color: Colors.blue,
                          fontWeight: FontWeight.bold)),
                  const Expanded(child: SizedBox()),
                  GestureDetector(
                    onTap: () {
                      viewModel
                          .unCollect("${itemData?.id}", index)
                          .then((value) {
                        if (value == true) {
                          showToast("取消收藏");
                        }
                      });
                    },
                    child: Image.asset("assets/images/a/ic_collect.png",
                        width: 35, height: 35),
                  ),
                ],
              )
            ],
          )),
    );
  }
}
